import PerfectScrollbar from "../components/Core/fixmodules/perfect-scrollbar/perfect-scrollbar";
import "perfect-scrollbar/css/perfect-scrollbar.css";

let scrollbars = [];
let resize = () => {
  for (let scrollbar of scrollbars) {
    if (scrollbar) {
      scrollbar.resize();
    }
  }
};
window.addEventListener("resize", resize);

class Scrollbar {
  constructor(refDom, config) {
    this.dom = refDom;
    if (!config) {
      this.config = {
        handlers: ["click-rail", "drag-thumb", "keyboard", "wheel", "touch"],
        swipeEasing: true,
        // wheelSpeed: 1,
        minScrollbarLength: 20
        // useBothWheelAxes: true
      };
    } else {
      this.config = config;
    }
    this.pstyle = "relative";
    this.ostyle = "hidden";
    this.scrollObj = null;
    scrollbars.push(this);
  }

  scrollTop(val) {
    this.dom.scrollTop = val ? val : 0;
  }

  scrollLeft(val) {
    this.dom.scrollLeft = val ? val : 0;
  }

  show() {
    if (this.scrollObj) {
      this.scrollObj.destroy();
    }
    this.dom.style.position = this.pstyle;
    this.dom.style.overflow = this.ostyle;
    this.scrollObj = new PerfectScrollbar(this.dom, this.config);
  }

  remove() {
    if (this.scrollObj) {
      this.scrollObj.destroy();
      this.scrollObj = null;
      this.dom.style.position = this.pstyle;
      this.dom.style.overflow = "auto";
    }
  }

  resize() {
    if (this.scrollObj) {
      this.scrollObj.update();
    } else {
      this.show();
    }
  }
}

export default Scrollbar;
